package com.hangzhou.welbeing.welbeinginstrument.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hangzhou.welbeing.welbeinginstrument.R;
import com.hangzhou.welbeing.welbeinginstrument.bean.MyCollectionBean;
import com.hangzhou.welbeing.welbeinginstrument.config.RBConstants;
import com.hangzhou.welbeing.welbeinginstrument.inteface.OnRecyclerViewItemClickListener;
import com.hangzhou.welbeing.welbeinginstrument.tool.GlideCircleTransform;

import java.util.ArrayList;
import java.util.List;

/**
 * 时间 2017 年 09 月 14.
 * 作者 Administrator  刘传华
 * 公司 杭州惟君医疗科技有限公司
 * 包名 com.hangzhou.welbeing.pregnant.welbeing_pregnant.adapter
 * 工程名 welbeing_pregnant
 */
public class MyCollectionAdapter extends  RecyclerView.Adapter<MyCollectionAdapter.ViewHolder> implements View.OnClickListener{
    List<MyCollectionBean.DataBean> mttdata  = new ArrayList<>();
    private Context mcontent ;
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
    public MyCollectionAdapter(List<MyCollectionBean.DataBean> data, Context content) {
        this.mttdata.addAll(data);
        this.mcontent = content;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_my_collection_recyclerview,parent,false);
        ViewHolder viewHolder = new ViewHolder(view);
        view.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.tv_title.setText(mttdata.get(position).po_name);
        holder.tv_content.setText(mttdata.get(position).content);
        holder.tv_name.setText(mttdata.get(position).cus_name);
        String po_url = mttdata.get(position).po_url;
        if(po_url.equals("")){
            holder.rl_picture.setVisibility(View.GONE);
        }else{
           String[] arr =  po_url.split(",");
            Glide.with(mcontent).load(RBConstants.SERVER_URL+"/Public/Uploads/"+arr[0]).into(holder.iv_picture);
        }
        Glide.with(mcontent).load(RBConstants.SERVER_URL+"/Public/Uploads/"+mttdata.get(position).cus_logo).transform(new GlideCircleTransform(mcontent)).into(holder.iv_head_portrait);
        holder.tv_circle.setText(mttdata.get(position).cat_name);


    }

    @Override
    public int getItemCount() {
        return mttdata.size();
    }

    @Override
    public void onClick(View v) {
        if(mOnItemClickListener != null ){
            mOnItemClickListener.onItemClick(v);
        }

    }

    /**
     * 身体布局Holder
     */
    static class ViewHolder extends RecyclerView.ViewHolder {

        private TextView tv_title;
        private  TextView tv_content;
        private  TextView tv_name;
        private  TextView tv_circle;

        private ImageView iv_picture;
        private ImageView iv_head_portrait;
        private RelativeLayout rl_picture;

        public ViewHolder(View itemView) {
            super(itemView);

            tv_title =  (TextView) itemView.findViewById(R.id.item_my_collection_recyclerview_tv_titile);
            tv_content =  (TextView) itemView.findViewById(R.id.item_my_collection_recyclerview_tv_content);
            tv_name =  (TextView) itemView.findViewById(R.id.item_my_collection_recyclerview_tv_name);
            tv_circle =  (TextView) itemView.findViewById(R.id.item_my_collection_recyclerview_tv_circle);

            iv_picture =  (ImageView) itemView.findViewById(R.id.item_my_collection_recyclerview_iv_picture);
            iv_head_portrait =  (ImageView) itemView.findViewById(R.id.item_my_collection_recyclerview_iv_head_portrait);
            rl_picture =  (RelativeLayout) itemView.findViewById(R.id.item_my_collection_recyclerview_rl_picture);
        }
    }
}
